package com.zkingsoft.laqeelin.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.matrix.core.constance.MatrixConstance;
import com.matrix.core.constance.SystemErrorCode;
import com.matrix.core.exception.GlobleException;
import com.matrix.core.pojo.PaginationVO;
import com.matrix.core.tools.ModelUtils;
import com.matrix.core.tools.UUIDUtil;
import com.matrix.core.tools.WebUtil;
import com.zkingsoft.common.bean.SysUsers;
import com.zkingsoft.laqeelin.bean.BizShare;
import com.zkingsoft.laqeelin.dao.BizShareDao;
import com.zkingsoft.laqeelin.service.BizShareService;

/**
 * @description service接口实现类(分享表)
 * @author 何明
 * @date 2018-09-01 10:35
 */
@Service
public class  BizShareServiceImpl implements  BizShareService{

	
	@Autowired
	private BizShareDao bizShareDao;
	
	
	@Override
	public int add(BizShare bizShare){
		// 设置基本字段信息
		SysUsers loginUser = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
		bizShare.setCreateBy(loginUser.getSuName());
		bizShare.setUpdateBy(loginUser.getSuName());
		bizShare.setShaId(UUIDUtil.getRandomID());
		return bizShareDao.insert(bizShare);
		
	}
	
	@Override
	public int batchAdd(List<BizShare>  bizShareList) {
		//这里没有做基本字段的设置，如有需要请自己实现	
		int num = 0;
		int c = 10000;
		int size = bizShareList.size()/c + 1;
		for(int i=0; i<size; i++) {
			int begin = i*c;
			int end = (i+1)*c;
			end = end >= bizShareList.size() ? bizShareList.size() : end;
			List<BizShare> insertList = bizShareList.subList(begin, end);
			num += bizShareDao.batchInsert(insertList);
		}
		return num;
		
	}
	
	
   	
    @Override
	public int modifyByMap(BizShare oldBizShare
	,BizShare newBizShare){
	
		Map<String, Object> modifyMap = null;
		try {
			if (!ModelUtils.isModified(oldBizShare, newBizShare)) {
				return MatrixConstance.DML_SUCCESSS;
			}
			modifyMap = ModelUtils.comparePojo2Map(oldBizShare, newBizShare);
		} catch (Exception e) {
			throw new GlobleException(SystemErrorCode.DATA_UPDATE_FAIL, e, newBizShare);
		}
		if (modifyMap.size() > 0) {
			modifyMap.put("shaId", oldBizShare.getShaId());
			bizShareDao.updateByMap(modifyMap);
		}
		return MatrixConstance.DML_SUCCESSS;
	}
	
	@Override
	public int modifyByModel(BizShare bizShare){
	
		return bizShareDao.updateByModel(bizShare);
	
	}
	
	
	
	@Override
	public int remove(List<String> list){
	
		return bizShareDao.deleteByIds(list);
	
	}

	@Override
	public int removeById(String shaId){
	
		return bizShareDao.deleteById(shaId);
	
	}
	
	@Override
	public int removeByModel(BizShare bizShare){
	
		return bizShareDao.deleteByModel(bizShare);
	
	}
	
	
	@Override
	public List<BizShare> findInPage(BizShare bizShare,  PaginationVO pageVo){
	
		return bizShareDao.selectInPage(bizShare , pageVo);
	
	}
	
	@Override
	public List<BizShare> findByModel(BizShare bizShare){
	
		return bizShareDao.selectByModel(bizShare);
	
	}
	
	@Override
	public int  findTotal(BizShare bizShare){
	
		return bizShareDao.selectTotalRecord(bizShare);
	
	}
	
	@Override
	public BizShare  findById(String shaId){
	
		return bizShareDao.selectById(shaId);
	
	}

   	
	
	
}